{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "84085029",
   "metadata": {},
   "source": [
    "# 1. Python 使用API 模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "10ac1218",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "02d595f1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'nszWQiTxf4n1D3-O39QvSqlqdaaj8KET'\n",
    "API_sercret = '48QuBUsMozhv02SL_v2UaZkeyEt6W4Nq'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4. 准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://s2.ax1x.com/2020/01/10/l4g0je.jpg'\n",
    "}\n",
    "\n",
    "# 5. 发起请求requests\n",
    "r = requests.post(url = base_url , params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7b09759c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3e37822f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648466187,1c08f635-4424-431d-89ee-212487df89c4',\n",
       " 'time_used': 2414,\n",
       " 'faces': [{'face_token': '41bca8dc84df073a6ba4bf464c6798f2',\n",
       "   'face_rectangle': {'top': 370, 'left': 255, 'width': 539, 'height': 539}}],\n",
       " 'image_id': 'kMHB/BGMXk3aE+2EDtb0XQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "358e69d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect = r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f8acc45c",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_tokens = face_detect['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "eb657d62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'41bca8dc84df073a6ba4bf464c6798f2'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c7bad0e",
   "metadata": {},
   "source": [
    "# 2. 人脸分析API\n",
    "* 请同学们使用上述模板合理调用函数来实现人脸分析API\n",
    "* 要求：获取return_attributes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "98d25408",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "Analyze_base_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'nszWQiTxf4n1D3-O39QvSqlqdaaj8KET'\n",
    "API_sercret = '48QuBUsMozhv02SL_v2UaZkeyEt6W4Nq'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4. 准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':face_tokens,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "}\n",
    "\n",
    "# 5. 发起请求requests\n",
    "r = requests.post(url = Analyze_base_url , params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9e06af79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4c76e8a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 172,\n",
       " 'request_id': '1648466190,53eb62ec-4b27-49b3-99cd-7965eb8f32b9',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.006,\n",
       "     'neutral': 99.708,\n",
       "     'disgust': 0.002,\n",
       "     'anger': 0.262,\n",
       "     'surprise': 0.013,\n",
       "     'fear': 0.008,\n",
       "     'happiness': 0.002},\n",
       "    'beauty': {'female_score': 87.525, 'male_score': 84.716},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 16},\n",
       "    'mouthstatus': {'close': 93.455,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 6.544,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 5.237,\n",
       "     'stain': 5.592,\n",
       "     'acne': 8.565,\n",
       "     'health': 59.438},\n",
       "    'headpose': {'yaw_angle': 3.034741,\n",
       "     'pitch_angle': 10.298685,\n",
       "     'roll_angle': 0.95849127},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.373},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.373},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.373}},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.013},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.0,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 100.0,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.0,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 100.0,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 89.473},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.449,\n",
       "      'vector_z_component': 0.989,\n",
       "      'vector_x_component': -0.052,\n",
       "      'vector_y_component': -0.138,\n",
       "      'position_y_coordinate': 0.367},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.503,\n",
       "      'vector_z_component': 0.997,\n",
       "      'vector_x_component': 0.032,\n",
       "      'vector_y_component': -0.076,\n",
       "      'position_y_coordinate': 0.399}}},\n",
       "   'face_rectangle': {'width': 539, 'top': 370, 'left': 255, 'height': 539},\n",
       "   'face_token': '41bca8dc84df073a6ba4bf464c6798f2'}]}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "031cf386",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a3cb953",
   "metadata": {},
   "source": [
    "## 1. 数据处理小魔法 pandas.json_normalize() 实现数据表格化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d62e6e1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>41bca8dc84df073a6ba4bf464c6798f2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>0.006</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>99.708</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>0.002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.262</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>0.013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>0.008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>0.002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>87.525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>84.716</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.close</th>\n",
       "      <td>93.455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.surgical_mask_or_respirator</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.open</th>\n",
       "      <td>6.544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.other_occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.glass.value</th>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.dark_circle</th>\n",
       "      <td>5.237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.stain</th>\n",
       "      <td>5.592</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.acne</th>\n",
       "      <td>8.565</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.health</th>\n",
       "      <td>59.438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>3.034741</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>10.298685</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>0.958491</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.value</th>\n",
       "      <td>0.373</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.value</th>\n",
       "      <td>0.373</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.value</th>\n",
       "      <td>0.373</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>0.013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_open</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.threshold</th>\n",
       "      <td>70.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.value</th>\n",
       "      <td>89.473</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.449</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_z_component</th>\n",
       "      <td>0.989</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.052</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.367</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.503</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_z_component</th>\n",
       "      <td>0.997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_x_component</th>\n",
       "      <td>0.032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.076</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>539</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>255</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>539</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                   0\n",
       "face_token                                          41bca8dc84df073a6ba4bf464c6798f2\n",
       "attributes.emotion.sadness                                                     0.006\n",
       "attributes.emotion.neutral                                                    99.708\n",
       "attributes.emotion.disgust                                                     0.002\n",
       "attributes.emotion.anger                                                       0.262\n",
       "attributes.emotion.surprise                                                    0.013\n",
       "attributes.emotion.fear                                                        0.008\n",
       "attributes.emotion.happiness                                                   0.002\n",
       "attributes.beauty.female_score                                                87.525\n",
       "attributes.beauty.male_score                                                  84.716\n",
       "attributes.gender.value                                                         Male\n",
       "attributes.age.value                                                              16\n",
       "attributes.mouthstatus.close                                                  93.455\n",
       "attributes.mouthstatus.surgical_mask_or_respirator                               0.0\n",
       "attributes.mouthstatus.open                                                    6.544\n",
       "attributes.mouthstatus.other_occlusion                                           0.0\n",
       "attributes.glass.value                                                          None\n",
       "attributes.skinstatus.dark_circle                                              5.237\n",
       "attributes.skinstatus.stain                                                    5.592\n",
       "attributes.skinstatus.acne                                                     8.565\n",
       "attributes.skinstatus.health                                                  59.438\n",
       "attributes.headpose.yaw_angle                                               3.034741\n",
       "attributes.headpose.pitch_angle                                            10.298685\n",
       "attributes.headpose.roll_angle                                              0.958491\n",
       "attributes.blur.blurness.threshold                                              50.0\n",
       "attributes.blur.blurness.value                                                 0.373\n",
       "attributes.blur.motionblur.threshold                                            50.0\n",
       "attributes.blur.motionblur.value                                               0.373\n",
       "attributes.blur.gaussianblur.threshold                                          50.0\n",
       "attributes.blur.gaussianblur.value                                             0.373\n",
       "attributes.smile.threshold                                                      50.0\n",
       "attributes.smile.value                                                         0.013\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                               0.0\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                               0.0\n",
       "attributes.eyestatus.left_eye_status.occlusion                                   0.0\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                             100.0\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                               0.0\n",
       "attributes.eyestatus.left_eye_status.dark_glasses                                0.0\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                               0.0\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                               0.0\n",
       "attributes.eyestatus.right_eye_status.occlusion                                  0.0\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                             100.0\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                               0.0\n",
       "attributes.eyestatus.right_eye_status.dark_glasses                               0.0\n",
       "attributes.facequality.threshold                                                70.1\n",
       "attributes.facequality.value                                                  89.473\n",
       "attributes.eyegaze.right_eye_gaze.position_x_co...                             0.449\n",
       "attributes.eyegaze.right_eye_gaze.vector_z_comp...                             0.989\n",
       "attributes.eyegaze.right_eye_gaze.vector_x_comp...                            -0.052\n",
       "attributes.eyegaze.right_eye_gaze.vector_y_comp...                            -0.138\n",
       "attributes.eyegaze.right_eye_gaze.position_y_co...                             0.367\n",
       "attributes.eyegaze.left_eye_gaze.position_x_coo...                             0.503\n",
       "attributes.eyegaze.left_eye_gaze.vector_z_compo...                             0.997\n",
       "attributes.eyegaze.left_eye_gaze.vector_x_compo...                             0.032\n",
       "attributes.eyegaze.left_eye_gaze.vector_y_compo...                            -0.076\n",
       "attributes.eyegaze.left_eye_gaze.position_y_coo...                             0.399\n",
       "face_rectangle.width                                                             539\n",
       "face_rectangle.top                                                               370\n",
       "face_rectangle.left                                                              255\n",
       "face_rectangle.height                                                            539"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d09f55aa",
   "metadata": {},
   "source": [
    "# 3. 人脸对比\n",
    "* 思考：人脸对比需要至少几张脸？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "331dcb99",
   "metadata": {},
   "source": [
    "## 1. 人脸检测获取toekn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "01146576",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'nszWQiTxf4n1D3-O39QvSqlqdaaj8KET'\n",
    "API_sercret = '48QuBUsMozhv02SL_v2UaZkeyEt6W4Nq'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4. 准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://s2.ax1x.com/2020/01/10/l4g0je.jpg'\n",
    "}\n",
    "\n",
    "# 5. 发起请求requests\n",
    "r = requests.post(url = base_url , params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "69013f5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'72e6c928d4da0762f929cf8b5b48cee9'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e93fd43",
   "metadata": {},
   "source": [
    "## 2. 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5c08ecaf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'nszWQiTxf4n1D3-O39QvSqlqdaaj8KET'\n",
    "API_sercret = '48QuBUsMozhv02SL_v2UaZkeyEt6W4Nq'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4. 准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://s2.ax1x.com/2020/01/10/l4g0je.jpg'\n",
    "}\n",
    "\n",
    "# 5. 发起请求requests\n",
    "r = requests.post(url = base_url , params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2b4c648f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ada891b86f812164d2063df87d79df58'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6af69034",
   "metadata": {},
   "source": [
    "## 2. 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "06e9fc16",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'nszWQiTxf4n1D3-O39QvSqlqdaaj8KET'\n",
    "API_sercret = '48QuBUsMozhv02SL_v2UaZkeyEt6W4Nq'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4. 准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':face_token01,\n",
    "    'face_token2':face_token02\n",
    "}\n",
    "\n",
    "# 5. 发起请求requests\n",
    "r = requests.post(url = compare_url , params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ef998cb4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "16b8fa64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648466196,4d608143-527b-4fe9-b865-22de7c008c78',\n",
       " 'time_used': 297,\n",
       " 'confidence': 97.389,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c01d9010",
   "metadata": {},
   "source": [
    "# 4. 封装人脸检测函数，实现快速调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "653b21bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'nszWQiTxf4n1D3-O39QvSqlqdaaj8KET'\n",
    "API_sercret = '48QuBUsMozhv02SL_v2UaZkeyEt6W4Nq'\n",
    "img_url_01 = 'https://s2.ax1x.com/2020/01/10/l4g0je.jpg'\n",
    "img_url_02 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3f41d765",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,image_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url_01\n",
    "    }\n",
    "    r = requests.post(url = base_url , params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1816b527",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648466196,39540d7b-2ed6-4450-98ad-289760ac3bce',\n",
       " 'time_used': 2084,\n",
       " 'faces': [{'face_token': '630517a0d727c6df8d5b5bdf17b1f157',\n",
       "   'face_rectangle': {'top': 370, 'left': 255, 'width': 539, 'height': 539}}],\n",
       " 'image_id': 'kMHB/BGMXk3aE+2EDtb0XQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "50365fb6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648466199,0a63fb57-636d-4afc-8e37-d2cfc378883d',\n",
       " 'time_used': 534,\n",
       " 'faces': [{'face_token': 'c0ac4d1f6fadf68ba57defa80814a909',\n",
       "   'face_rectangle': {'top': 370, 'left': 255, 'width': 539, 'height': 539}}],\n",
       " 'image_id': 'kMHB/BGMXk3aE+2EDtb0XQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c984fff",
   "metadata": {},
   "source": [
    "## 实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e22ff254",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "15f4f27a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'nszWQiTxf4n1D3-O39QvSqlqdaaj8KET'\n",
    "API_sercret = '48QuBUsMozhv02SL_v2UaZkeyEt6W4Nq'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4. 准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "}\n",
    "\n",
    "# 5. 发起请求requests\n",
    "r = requests.post(url = base_url , params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "b60d4c50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8e89d265",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'd0637b914ec7c1a80af356fac6bdf684',\n",
       " 'time_used': 173,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648466199,d55d5823-315f-4700-9bc9-aa98abd58366',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "180bd051",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09204759",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'nszWQiTxf4n1D3-O39QvSqlqdaaj8KET'\n",
    "API_sercret = '48QuBUsMozhv02SL_v2UaZkeyEt6W4Nq'\n",
    "img_url_01 = 'https://s2.ax1x.com/2020/01/10/l4g0je.jpg'\n",
    "img_url_02 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64c2b14d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
